package cn.yunhe.crm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.yunhe.crm.annotation.SystemLog;
import cn.yunhe.crm.dao.EmpMapper;
import cn.yunhe.crm.entity.Contact;
import cn.yunhe.crm.entity.Emp;
import cn.yunhe.crm.service.ContactService;

@Scope("prototype")
@Controller
public class ContactController {
	@Autowired
	private EmpMapper eMapper;
	
	public void seteMapper(EmpMapper eMapper) {
		this.eMapper = eMapper;
	}

	@Autowired
	@Qualifier("ContactBiz")
	private ContactService contactService;
	
	public void setContactService(ContactService contactService) {
		this.contactService = contactService;
	}

	/**
	 * 查询所有联系人
	 * @return
	 */
	@RequestMapping("/contact.do")
	public String ContactManage(HttpSession session){
		List<Contact> contactlist=contactService.selectByExample();
		session.setAttribute("contactlist", contactlist);
		return "contactmanage";
	}
	/**
	 * 查看联系人详情
	 * @return
	 */
	@SystemLog(des="查看联系人")
	@RequestMapping("/contactmanagedetail.do")
	public String ContactManagedetail(Integer id,HttpSession session){
		Contact contact=contactService.findContactdetail(id);
		session.setAttribute("contact", contact);
		return "contactmanagedetail";
	}
	
	@RequestMapping("detailback.do")
	public String detailback(){
		return "redirect:/contact.do";
	}
	
	@SystemLog(des="删除联系人")
	@RequestMapping("/delContactById.do")
	public String delContactById(Integer id){
		int flag=contactService.editContactStateById(id);
		if(flag>0){
			return "redirect:/contact.do";
		}else{
			return "null";
		}
	}
	/**
	 * 跳转至新增页面
	 * @return
	 */
	@RequestMapping("/getcontactmanageadd.do")
	public String contactmanageadd(HttpSession session){
		List<Emp> list=eMapper.checkEmpnameByAllot();
		session.removeAttribute("emps");
		session.setAttribute("emps", list);
		return "contactmanageadd";
	}
	/**
	 * 新增联系人
	 * @param record
	 * @return
	 */
	@SystemLog(des="新增联系人")
	 @RequestMapping("/contactmanageadd.do")
	public String contactmanageadd(Contact record){
		
		int flag=contactService.insertContact(record);
		if(flag>0){
			return "redirect:/contact.do";
		}
			return "contactmanageadd";
	}
	
	/**
	 * 跳转至编辑页面
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping("/getcontactmanageupdate.do")
	public String getcontactupdate(Integer id,HttpSession session){
		Contact contact=contactService.selectByPrimaryKey(id);
		session.removeAttribute("contact");
		session.setAttribute("contact", contact);
		List<Emp> list=eMapper.checkEmpnameByAllot();
		session.removeAttribute("emps");
		session.setAttribute("emps", list);
		return "contactmanageupdate";
	}
	
	/**
	 * 更新联系人
	 * @param id
	 * @param contact
	 * @return
	 */
	@SystemLog(des="更新联系人")
	@RequestMapping("/contactmanageupdate.do")
	public String contactupdate(Integer id,Contact contact){ 
		
		int flag=contactService.updateContactById(contact);
			
		return "redirect:/contact.do";
	}
	
	/**
	 * 手机唯一性验证
	 * @param phone
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/phonevalidate.do")
	public Object findContactByPhone(String phone){
		Contact contact =contactService.findContactByPhone(phone);
		Map map = new HashMap();
		if(contact !=null){
			map.put("res", "true");
			map.put("id", contact.getId());
			map.put("ename", contact.getName());
			map.put("phone", contact.getPhone());
			
		}else {
			map.put("res", "false");
		}
		return JSONArray.toJSONString(map);
	}
	/**
	 * 通过经办人姓名获取经办人手机
	 */
	@ResponseBody
	@RequestMapping("checkEphoneByName.do")
	public Object checkEphoneByName(String ename){
		Emp emp=eMapper.checkPhoneByName(ename);
		Map map=new HashMap();
		if(emp!=null){
			map.put("ephone", emp.getPhone());
		}
		return JSONObject.toJSON(map);
	}
	
}
